use criterion::{black_box, criterion_group, criterion_main, Criterion};
use cdb::sql::parser::SqlParser;

/// 基准测试：SQL解析器性能
fn benchmark_sql_parser(c: &mut Criterion) {
    let parser = SqlParser::new();
    
    // 简单SELECT查询
    c.bench_function("parse_simple_select", |b| {
        b.iter(|| {
            let sql = black_box("SELECT id, name FROM users WHERE age > 18");
            parser.parse(sql).unwrap()
        })
    });
    
    // 复杂JOIN查询
    c.bench_function("parse_complex_join", |b| {
        b.iter(|| {
            let sql = black_box(
                "SELECT u.name, p.title, COUNT(c.id) as comment_count 
                 FROM users u 
                 JOIN posts p ON u.id = p.user_id 
                 LEFT JOIN comments c ON p.id = c.post_id 
                 WHERE u.created_at > '2023-01-01' 
                 GROUP BY u.id, u.name, p.id, p.title 
                 HAVING COUNT(c.id) > 5 
                 ORDER BY comment_count DESC 
                 LIMIT 10"
            );
            parser.parse(sql).unwrap()
        })
    });
    
    // INSERT语句
    c.bench_function("parse_insert", |b| {
        b.iter(|| {
            let sql = black_box("INSERT INTO users (id, name, email, age) VALUES (1, 'Alice', 'alice@example.com', 25)");
            parser.parse(sql).unwrap()
        })
    });
    
    // UPDATE语句
    c.bench_function("parse_update", |b| {
        b.iter(|| {
            let sql = black_box("UPDATE users SET name = 'Bob', age = 26 WHERE id = 1");
            parser.parse(sql).unwrap()
        })
    });
    
    // CREATE TABLE语句
    c.bench_function("parse_create_table", |b| {
        b.iter(|| {
            let sql = black_box(
                "CREATE TABLE users (
                    id INTEGER PRIMARY KEY,
                    name VARCHAR(100) NOT NULL,
                    email VARCHAR(255) UNIQUE,
                    age INTEGER CHECK (age >= 0),
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                )"
            );
            parser.parse(sql).unwrap()
        })
    });
    
    // 多语句解析
    c.bench_function("parse_multiple_statements", |b| {
        b.iter(|| {
            let sql = black_box(
                "SELECT * FROM users; 
                 INSERT INTO users VALUES (1, 'Alice'); 
                 UPDATE users SET name = 'Bob' WHERE id = 1;"
            );
            parser.parse(sql).unwrap()
        })
    });
}

/// 基准测试：SQL分析功能
fn benchmark_sql_analysis(c: &mut Criterion) {
    let parser = SqlParser::new();
    
    c.bench_function("analyze_complex_query", |b| {
        b.iter(|| {
            let sql = black_box(
                "SELECT u.name, p.title, COUNT(c.id) as comment_count 
                 FROM users u 
                 JOIN posts p ON u.id = p.user_id 
                 LEFT JOIN comments c ON p.id = c.post_id 
                 WHERE u.created_at > '2023-01-01' 
                 GROUP BY u.id, u.name, p.id, p.title 
                 HAVING COUNT(c.id) > 5 
                 ORDER BY comment_count DESC 
                 LIMIT 10"
            );
            parser.analyze(sql).unwrap()
        })
    });
}

/// 基准测试：表名提取
fn benchmark_table_extraction(c: &mut Criterion) {
    let parser = SqlParser::new();
    
    c.bench_function("extract_table_names", |b| {
        b.iter(|| {
            let sql = black_box(
                "SELECT u.name, p.title FROM users u JOIN posts p ON u.id = p.user_id"
            );
            parser.extract_table_names(sql).unwrap()
        })
    });
}

criterion_group!(
    benches,
    benchmark_sql_parser,
    benchmark_sql_analysis,
    benchmark_table_extraction
);
criterion_main!(benches);
